// 1.安装 npm i vue-router
// 2. 导入
import VueRouter from 'vue-router'
// 3. 注册
import Vue from 'vue'

const Login = () => import('@/views/login')
const Layout = () => import('@/views/Layout')
const Home = () => import('@/views/home')
const Article = () => import('@/views/article')
const Publish = () => import('@/views/publish')

Vue.use(VueRouter)
// 4. 初始化
const router = new VueRouter({
  routes: [
    { path: '/login', component: Login },
    {
      path: '/',
      component: Layout,
      children: [
        { path: '/', component: Home },
        { path: '/article', component: Article },
        { path: '/publish', component: Publish }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  // 获取token
  const token = localStorage.getItem('geek-client-pc-store')
  // 不是访问登录，有没有token，跳转登录页面
  if (to.path !== '/login' && !token) return next('/login')
  // 其他情况放行
  next()
})

// 5. 导出实例
export default router
